$(document).ready(function (){

require(['inputdrop',
		'tree',
		'tab',
		'plot',
		'workorder',
		'event'
	], function(inputdrop, tree, tab, plot, workorder, pevent) {

event_init();

var el_event_list = $("#table_div");

var searchObj = new Object();

var vagueObj = new Object();

var vagueFlag = false;

var ignoreFlag = false;

pevent.faultEventList();

function event_init()
{
	//模糊查询
	$(".vague-btn").click(function(){
		$("#vague_hidden").val("1")
		if($("#vague_div").val() == "")
		{
			g_dialog.operateAlert($("#table_oper"),"请输入关键词。","error");
			return false;
		}
		pevent.faultEventList();
	})

	//自定义列
	$("[id=custom_btn]").click(function (){
		pevent.eventCustomCol($(this) ,{
			tplUrl : "module/event_alarm/fault_event_analy_tpl.html",
			colQueryUrl : "faultAlarmEvent/queryFaultColumeIds",
			customColumnsUrl : "faultAlarmEvent/saveFaultCustomColumn",
			type:"2",
			cbf : function (){
				pevent.faultEventList();
			}
		});
	});

	// 导出按钮
	$("[data-val=fault_event_li]").find("[id=export_btn]").click(function(rowData) {
		var queryObj = $("#table_div").data("opt").paramObj;
		pevent.eventExport({
			attr : "faultNO",
			url : "faultAlarmEvent/exportFault",
			queryObjStr : JsonTools.encode(queryObj)
		});
	});

	// 统计
	$("#count_btn").click(function (){
		//切换到统计图展示
		//searchObj = $("#table_div").children().data("queryObj");
		$("#table_div").css("height" ,"auto")
		$("#table_oper").css("border-bottom" ,"none")
		searchObj = $("[class*=um-grid-search-tr]").umDataBind("serialize" ,"search-data");
		count_table_init();
		$(this).toggle();
		$("#grid_btn").toggle();
	});
	$("#grid_btn").click(function (){
	   //切换到列表展示
		pevent.faultEventList({pObj:searchObj});
		$(this).toggle();
		$("#count_btn").toggle();
	});

	var perform_event_stat_url = "performanceEvent/statPerformanceEventQuery";

	// 忽略
	$("[data-val=fault_event_li]").find("[id=lgnore_btn]").click(function() {
		var array = g_grid.getIdArray(el_event_list ,{chk:true ,attr:"faultStatus"});
		if (array.join("").indexOf("2") >= 0)
		{
			g_dialog.operateAlert(el_event_list ,"选中记录中包含已忽略的记录，请重新选择","error");
			return false;
		}
		pevent.ignore({
			gridEl: el_event_list,
			attr: "faultNO",
			ignore_url: "faultAlarmEvent/doIgnore",
			cb: function() {
				g_grid.refresh(el_event_list);
			}
		});
	});

	// 忽略全部
	$("[data-val=fault_event_li]").find("[id=lgnore_all_btn]").click(function(rowData) {
		var data = g_grid.getData($("#table_div") ,{chk:false});
		if (data.length == 0)
		{
			// 弹出提示
			g_dialog.operateAlert($("#table_div") ,"列表无数据。" ,"error");
			// 直接返回
			return false;
		}
		pevent.ignoreAll({
			gridEl: el_event_list,
			lgnore_all_url: "faultAlarmEvent/doFaultEventIgnoreAll",
			cb: function() {
				pevent.faultEventList();
			}
		});
	});

	// 生成工单
	$("[data-val=fault_event_li]").find("[id=order_btn]").click(function() {
		pevent.createWorkOrder({
			gridEl : $("#table_div"),
			descKey : "faultModule",
			eventIdKey : "faultNO",
			eventTypeVal : "2"
		});
	});

	index_list_search_render($("um-search") ,{
		cbf : function (val){
			g_grid.refresh($("#table_div") ,{
				paramObj : {faultName : val}
			})
		},
		placeholder : "请输入事件名称"
	})

	g_formel.list_filter_render($("um-filter") ,{
		condition : [
						{text : "当前状态" ,name : "currentStatus" ,data : [{text:"正常",value:"1"} ,{text:"异常",value:"0"}]},
						{text : "状态" ,name : "faultStatus" ,data : [{text:"未处理",value:"1"} ,{text:"忽略",value:"2"},
																	 {text:"处理中",value:"3"} ,{text:"已处理",value:"4"}]}
					],
		click : function (paramObj){
			g_grid.refresh($("#table_div") ,{
				paramObj : paramObj
			})
		}
	})
}

function count_table_init()
{
	var fault_event_stat_url = "faultAlarmEvent/statQuery";
	el_event_list.html("");
	$.ajax({
		type: "GET",
		url: "module/event_alarm/fault_event_analy_tpl.html",
		success :function(data)
		{
			el_event_list.html($(data).find("[id=count_tem]"));
			chart_render(el_event_list.find("[id=eventname_analy_chars]"), 1 ,"故障事件统计" ,fault_event_stat_url);
			chart_render(el_event_list.find("[id=type_analy_chars]"), 2 ,"故障事件统计" ,fault_event_stat_url);
			chart_render(el_event_list.find("[id=level_analy_chars]"), 6 ,"故障事件统计" ,fault_event_stat_url);
			chart_render(el_event_list.find("[id=time_analy_chars]"), 7 ,"故障事件统计" ,fault_event_stat_url);
		}
	});
}

function chart_render(el, typeid ,seriesName ,url) {
	// var paramObj = $("[class*=um-grid-search-tr]").umDataBind("serialize" ,"search-data");
	var paramObj = new Object();
	paramObj = $.extend(paramObj, searchObj);
	paramObj.queryFlag = true;
	paramObj.typeid = typeid;
	// if (!paramObj.currentStatus || paramObj.currentStatus == "-1")
	// {
	// 	paramObj.currentStatus = 0;
	// }
	if (typeid == "7")
	{
		um_ajax_get({
			url: url,
			paramObj: paramObj,
			isLoad : false,
			successCallBack: function(data) {
				var seriesArray = [];
				var categoryArray = [];
				seriesObj = new Object();
				seriesObj.name = seriesName;
				seriesObj.type = "line";
				//seriesObj.areaStyle = {normal: {}};
				seriesObj.data = [];
				var data = data.datas[0].items;
				for (var i = 0; i < data.length; i++) {
					categoryArray.push(data[i].lable);
					seriesObj.data.push(data[i].value);
				}
				seriesArray.push(seriesObj);
				plot.lineRender(el, {
					category: categoryArray,
					series: seriesArray,
					grid: {
				        left: '6%',
				        right: '9%',
				        bottom: '3%',
				        containLabel: true
				    }
				});
			}
		});
	}
	else
	{
		var color_array = ['#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'];
		um_ajax_get({
			url: url,
			paramObj: paramObj,
			isLoad : false,
			successCallBack: function(data) {
				var seriesArray = [];
				var seriesObj = new Object();
				seriesObj.name = "值";
				seriesObj.type = "bar";
				seriesObj.data = [];
				seriesObj.itemStyle = new Object();
				seriesObj.itemStyle.normal = new Object();
				seriesObj.itemStyle.normal.color = color_array[typeid];
				var categoryArray = [];
				var data = data.datas;
				for (var i = 0; i < data.length; i++) {
					seriesObj.data.push(data[i].value);
					categoryArray.push(data[i].lable);
				}
				seriesArray.push(seriesObj);
				plot.barRender(el, {
					category: categoryArray,
					series: seriesArray,
					isVercital: true
				});
			}
		});
	}
}

});
});